/*-----------------------------------------------------
 * File: GUI_Run.c
 *
 * Purpose:  Gestione GUI
 *
 *
 * Description:  
 * 
 * Version: 1.0-200112281649
 *             
 * Copyright 2001 ArSilicii srl
 *-----------------------------------------------------*/

#include  <stdio.h>      
#include  <math.h>
#include "hal.h"   
#include "InParser.h"
#include "OutDrv.h"
#include "capture.h"
#include "GUI_Run.h"

char DBuffer[10];
struct tGUI_Run_Event GUI_Run_Event;
enum eGUI_Run_State  {gBoot,gPostBoot,Standby,Setup,Run,Canal1,Canal2,Alarm,Admin} GUI_Run_State;

//------------- Menu Admin
#define Max_Menu_Item (4)
#define Menu_Exit_Timeout (500)
const char *GUI_Menu_Admin[]={ "SA= ","SC= ","TS= ","TC= "};
const unsigned int GUI_Menu_Param_Limit_Max[]= { 9999,9999,9999,(255*Capture_WM_Clock/10)};
const unsigned int GUI_Menu_Param_Limit_Min[]= { 0,0,0,Capture_WM_Clock/10+1};
const char *GUI_Menu_Admin_Form[]={ "%4.0f","%4.0f","%5.1f","%5.2f"};
const char *GUI_Menu_Admin_Form_Edit[]={ "%04.0f","%04.0f","%05.1f","%05.2f"};
const float GUI_Menu_Admin_Factor[]={ 1.0,1.0,0.1,0.01};

enum eGUI_Run_Admin_State {M_Init,M_Show,M_Nav,M_NavValue,M_EndNav,M_Modify} GUI_Run_Admin_State;

void   GUI_Run_Init(void) {
       GUI_Run_State=gBoot;
       GUI_Run_Admin_State=M_Init;
       DBuffer[0]=0x00;
}

unsigned char   GUI_Run_Admin_Clk(void) {
    static unsigned char MItem,Digit,tDelay;
    static unsigned int Param[Max_Menu_Item];
    static unsigned int tenpower,timeout;
    unsigned int curDigit;
    struct tFLASHRAM tempNV;
            
    if(InParser.Value.name.OK & InParser.Event.name.OK){       
        return(1);
        }
    
        
    
    switch(GUI_Run_Admin_State) {
       case M_Init:
          timeout=Menu_Exit_Timeout;
          tDelay=MItem=Digit=0;          
          sprintf(DBuffer,"MENU");
          Disp_string(DBuffer);
          Param[0]=NVRAM.SogliaH;
          Param[1]=NVRAM.SogliaL;
          Param[2]=NVRAM.TStdby;
          Param[3]=(NVRAM.Tfmm*Capture_WM_Clock)/10;  
          
          GUI_Run_Admin_State=M_Show;
          break;
       
       
       case M_Show:          
          if(tDelay++ > 8){
               tDelay=0;
               GUI_Run_Admin_State=M_Nav;          
          }
          break;
          
       case M_Nav:          
          sprintf(DBuffer,GUI_Menu_Admin[MItem]);
          Disp_string(DBuffer);
          if(InParser.Value.name.P1 & InParser.Event.name.P1)GUI_Run_Admin_State=M_Modify;
          if(InParser.Value.name.P2 & InParser.Event.name.P2){
               tDelay=0;
               MItem++;
          }
          
          if(tDelay++ >= 6){
               tDelay=0;
               GUI_Run_Admin_State=M_NavValue; 
          }  
          
          if(MItem>=Max_Menu_Item){
               MItem=0;
               GUI_Run_Admin_State=M_EndNav; 
          }

              
          break;
       
       case M_NavValue:
          
          if(InParser.Value.name.P1 & InParser.Event.name.P1)GUI_Run_Admin_State=M_Modify;
          sprintf(DBuffer,GUI_Menu_Admin_Form[MItem],Param[MItem]*GUI_Menu_Admin_Factor[MItem]);
          if(tDelay++ >= 8){
               tDelay=0;
               GUI_Run_Admin_State=M_Nav;
          }  
          Disp_string(DBuffer);
          if(InParser.Value.name.P2 & InParser.Event.name.P2){               
               MItem++;
               if(MItem>=Max_Menu_Item){
                GUI_Run_Admin_State=M_EndNav; 
               }
          }

          break;                    
          
       case M_EndNav:
          sprintf(DBuffer,"EXIT");
          Disp_string(DBuffer);
          
          if(InParser.Value.name.P2 & InParser.Event.name.P2){
               tDelay=0;
               MItem=0;
               GUI_Run_Admin_State=M_Nav;
          }

          if(InParser.Value.name.P1 & InParser.Event.name.P1){ 
               tempNV.SogliaH=Param[0];
               tempNV.SogliaL=Param[1];
               tempNV.TStdby=Param[2];
               tempNV.Tfmm=(Param[3]*10)/Capture_WM_Clock;
               
               HAL_NV_Write(NVRAM,tempNV);
               Capture_Init();                              
               return(1);
          }     
          break;
       
       case M_Modify:
          if(Digit==0) tenpower=1;          
          sprintf(DBuffer,GUI_Menu_Admin_Form_Edit[MItem],Param[MItem]*GUI_Menu_Admin_Factor[MItem]);
          Disp_Flashing_string(DBuffer,Digit);                    
          if(InParser.Value.name.P1 & InParser.Event.name.P1){
              tenpower*=10;
              Digit++;
          }
          
          if(Digit>=4){
               Digit=0;
               tDelay=0;
               GUI_Run_Admin_State=M_Nav;
          }               

          if(InParser.Value.name.P2 & InParser.Event.name.P2){
              curDigit= ((Param[MItem]/tenpower) % 10);
              if(Param[MItem]==GUI_Menu_Param_Limit_Max[MItem])Param[MItem]-= curDigit*tenpower; 
              else if (curDigit==9){
                Param[MItem]-= 9*tenpower;
              } else {
                Param[MItem]+= tenpower;
              }
              if(Param[MItem]>GUI_Menu_Param_Limit_Max[MItem])Param[MItem]= GUI_Menu_Param_Limit_Max[MItem];
              if(Param[MItem]<GUI_Menu_Param_Limit_Min[MItem])Param[MItem]= GUI_Menu_Param_Limit_Min[MItem];
          }
          break;
    }
    if(InParser.Event.all) timeout=Menu_Exit_Timeout;    
    if((timeout--) ==0){               
        return(1);
    }
    return(0);
}

void GUI_Run_Clk(void) {
static char gDelay;
    
//-----------------       
  if(GUI_Run_State>=Standby){    
    if(GUI_Run_State!=Admin){
     Disp_string(DBuffer);
     if(GUI_Run_Event.Alarm)GUI_Run_State=Alarm; 
     if(!GUI_Run_Event.Start)GUI_Run_State=Standby;
    }
  }else Disp_string(DBuffer);  
//-----------------

    switch(GUI_Run_State) {
        
       case gBoot:
          LED.DL2=LED.DL1=0;
          gDelay=10;
          GUI_Run_State=Standby;
          if(InParser.Value.name.P1)GUI_Run_State=gPostBoot;
          break;

        
       case gPostBoot:
          sprintf(DBuffer,"R1.0B "); 
          LED.DL2=LED.DL1=0;
          if((gDelay--) == 0)GUI_Run_State=Standby;          
          break;

       case Standby:
          sprintf(DBuffer," . . . ."); 
          LED.DL2=LED.DL1=0;
          if(GUI_Run_Event.Start)GUI_Run_State=Setup;
          if(InParser.Value.name.OK && InParser.Event.name.OK)GUI_Run_State=Admin;
          break;
          
       case Setup:          
          sprintf(DBuffer,"%4.0f",Capture_Read_Diff());
          if(GUI_Run_Event.Run)GUI_Run_State=Run;
          if(InParser.Value.name.P1)sprintf(DBuffer,"%4f",Capture_Read_F1());
          if(InParser.Value.name.P2)sprintf(DBuffer,"%4f",Capture_Read_F2());
          LED.DL2^=1; 
          break;
          
       case Run:
          LED.DL2=LED.DL1=0;
          sprintf(DBuffer,"%5.1f",Capture_Read_Diff());
          if(InParser.Value.name.P1)GUI_Run_State=Canal1;
          if(InParser.Value.name.P2)GUI_Run_State=Canal2;
          break;
           
       case Canal1:
          //sprintf(DBuffer,"-C1-");
          sprintf(DBuffer,"%4f",Capture_Read_F1());
          if(!InParser.Value.name.P1)GUI_Run_State=Run;
          break;

       case Canal2:
          //sprintf(DBuffer,"-C2-");
          sprintf(DBuffer,"%4f",Capture_Read_F2());
          if(!InParser.Value.name.P2)GUI_Run_State=Run;
          break;

       case Alarm:
          if(!GUI_Run_Event.Alarm)GUI_Run_State=Run;          
          sprintf(DBuffer,"    ");          
          LED.DL1^=1;
          if (LED.DL1){ 
          sprintf(DBuffer,"%5.1f",Capture_Read_Diff());
          if(InParser.Value.name.P1)sprintf(DBuffer,"%4f",Capture_Read_F1());
          if(InParser.Value.name.P2)sprintf(DBuffer,"%4f",Capture_Read_F2());
          }
          break;
          
       case Admin:
          if(GUI_Run_Admin_Clk()) {
              GUI_Run_State=Standby;       
              GUI_Run_Admin_State=M_Init;
          }
          break;          
    }    

}

